package com.vecspace.security.proxy.httphandler;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;

import com.vecspace.constant.AppConst;
import com.vecspace.exception.HException;
import com.vecspace.security.proxy.service.AuthorizeService;
import com.vecspace.security.proxy.service.LoginManager;
import com.vecspace.sso.entity.AppFunctionAuth;
import com.vecspace.web.servlet.model.HController;
import com.vecspace.web.servlet.model.Param;
import com.vecspace.web.servlet.model.Path;

/**获取用户的授权相关的信息
 * 
 * @version: 1.0
 * @author: wanghua
 */
@HController("authHandler")
public class AuthHandler {
	
	@Autowired
	private AuthorizeService authService;
	
	@Path("login")
	public String login(@Param("userId")String userId, @Param("password")String password) throws Exception{
		String token = authService.login(userId, password);
		return token;
	}
	
	/**
	 * 返回子节点及所有级别的孙节点
	 * 功能描述：
	 * @param parentFuncId
	 * @return
	 * @throws Exception
	 * @author: wanghua
	 */
	@Path("getAuthFunctionTree")
	public List<AppFunctionAuth> getAuthFunctionTree(@Param("parentFuncId")String parentFuncId) throws Exception{
		List<AppFunctionAuth> list = LoginManager.getAuthFunctionTree(parentFuncId);		
		return list;
	}
	
	/**获取所有授权的菜单项
	 * 根据application.properties文件中定义的APP.RootFunID来获取
	 * */
	@Path("getAuthFunctionTreeAll")
	public List<AppFunctionAuth> getAuthFunctionTreeAll() throws Exception{
		String parentFuncId = AppConst.getSystemRootMenuCode();
		List<AppFunctionAuth> list = LoginManager.getAuthFunctionTree(parentFuncId);		
		return list;
	}
	
	@Path("resetPassword")
	public boolean resetPassword(@Param("oldPassword")String oldPassword, @Param("newPassword")String newPassword) throws Exception{
		boolean validateMsg = false; 
		String logonId = LoginManager.getLoginId();
		if(logonId == null)throw new HException("没有登录，不能修改密码");
		validateMsg = authService.resetPassword(logonId, oldPassword, newPassword);
		return validateMsg;
	}
	public AuthorizeService getAuthService() {
		return authService;
	}

	public void setAuthService(AuthorizeService authService) {
		this.authService = authService;
	}
}
